* Firm organization with multiple establishments
* Section V: Appendix Table D.12
* Directly affected establishments

clear all 
set matsize 2000
set more off

capture log close
log using log/22a_train_layer-firm_est-direct_romanowolf.log, replace

********************************************************************************
***	Full sample, Table D.12, top left

use data/train_analysis_firm.dta, clear

keep if hauptbet == 0 & d_traindata == 1

cap drop flg_est_cty
egen flg_est_cty = tag(betnr ao_kreis)
bys betnr: egen cty_per_est = total(flg_est_cty)

keep if cty_per_est == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
tab2 aux_touse touse
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
cap drop num flg_strata

set seed 2021

**** Outcome variables

local var1 ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet

*** Run and save regression on full sample for comparison to bootstrapped samples

local i=1
foreach var of local var1 {

	reghdfe `var' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
	local c`i'=_b[faster1]
	local s`i'=_se[faster1]
	local ++i
}

*** Create empty matrix to save results from bootstrapping

foreach var of local var1 {
		matrix m_`var' = (.,.,.,.)
		matrix colnames m_`var' = "b" "ser" "pv" "Nobs"
	}

cap drop weight

**** Bootstrap strata, estimate coefficients, save coefficients and standard errors

forvalues n = 1/300 {
	preserve

	qui {
	// Sample strata with replacement
	bsample if touse == 1, cluster(ao_kreis)

	cap drop sample
	qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
	qui gen sample = e(sample)

	cap drop touse*
	cap drop weight*

	bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
	gen tmp8 = (tmp7 > 0)
	by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
	gen tmp10 = (tmp9 > 0)

	cap drop touse
	gen touse = (tmp8 > 0 & tmp10 > 0)
	tab2 aux_touse touse
	qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

	cap drop n_treat_s n_ec_s
	bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
	bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

	cap drop aux_weight
	gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
	replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

	sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
	local n_treat = `r(N)'

	sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
	local n_ec = `r(N)' 

	gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
	replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

	tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

	qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
	tabstat weight if e(sample), c(s) s(N sum)

	bys group_strata: egen num = total(touse * sample)
	egen flg_strata = tag(group_strata)
	tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

	drop aux_weight 
	drop n_treat_s n_ec_s
	drop tmp*
	cap drop num flg_strata

	// Run regression on bootstrapped sample

	foreach var of varlist `var1' {

		qui reghdfe `var' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)

		// Save results in matrix; saving coefficient, standard error, p-value and number of observations
		matrix m_`var' = (m_`var' \ _b[faster1], _se[faster1], ///
								2*ttail(e(df_r), abs(_b[faster1]/_se[faster1])), e(N))
	

	}
	}
	restore

}


foreach var of varlist `var1' {

	matrix list m_`var'
	svmat double m_`var', name(`var')

	drop `var'3 `var'4
	rename `var'1 `var'_beta
	rename `var'2 `var'_se
	}

local allcorrs `c1' `c2' `c3' `c4'
local allserrs `s1' `s2' `s3' `s4'

***	Table D.12, top left, P-values Romano-Wolf
rwolf ln_prdt_bet_beta avg_lnw_bot_bet_beta count_mgmt_bet_beta shr_bloss_w_bet_beta, ///
	nobootstraps stdests(ln_prdt_bet_se avg_lnw_bot_bet_se count_mgmt_bet_se shr_bloss_w_bet_se) pointestimates(`allcorrs') stderrs(`allserrs')


********************************************************************************
***	Firms with at least two establishments, Table D.12, top right

use data/train_analysis_firm.dta, clear

keep if hauptbet == 0 & d_traindata == 1 & count_est > 2

cap drop flg_est_cty
egen flg_est_cty = tag(betnr ao_kreis)
bys betnr: egen cty_per_est = total(flg_est_cty)

keep if cty_per_est == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
bysort group_strata: egen tmp1 = sum(ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0)

cap drop sample
qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
qui gen sample = e(sample)

cap drop touse*
cap drop weight*

bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0)
tab2 aux_touse touse
qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)

bys group_strata: egen num = total(touse * sample)
egen flg_strata = tag(group_strata)
tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
cap drop num flg_strata

set seed 2021

**** Outcome variables

local var1 ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet

*** Run and save regression on full sample for comparison to bootstrapped samples

local i=1
foreach var of local var1 {

	reghdfe `var' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)
	local c`i'=_b[faster1]
	local s`i'=_se[faster1]
	local ++i
}

*** Create empty matrix to save results from bootstrapping

foreach var of local var1 {
		matrix m_`var' = (.,.,.,.)
		matrix colnames m_`var' = "b" "ser" "pv" "Nobs"
	}

cap drop weight

**** Bootstrap strata, estimate coefficients, save coefficients and standard errors

forvalues n = 1/300 {
	preserve

	qui {
	// Sample strata with replacement
	bsample if touse == 1, cluster(ao_kreis)

	cap drop sample
	qui reghdfe ln_empl_bet faster1 if aux_touse == 1, absorb(betnr county_year) vce(robust)
	qui gen sample = e(sample)

	cap drop touse*
	cap drop weight*

	bys group_strata: egen tmp7 = sum(ever_faster1 == 0 & sample == 1)
	gen tmp8 = (tmp7 > 0)
	by  group_strata: egen tmp9 = sum(ever_faster1 == 1 & sample == 1)
	gen tmp10 = (tmp9 > 0)

	cap drop touse
	gen touse = (tmp8 > 0 & tmp10 > 0)
	tab2 aux_touse touse
	qui reghdfe ln_empl_bet faster1 if touse == 1, absorb(betnr county_year) vce(robust)

	cap drop n_treat_s n_ec_s
	bysort group_strata: egen n_treat_s = sum(ever_faster1 == 1 & sample == 1)
	bysort group_strata: egen n_ec_s = sum(ever_faster1 == 0 & sample == 1)

	cap drop aux_weight
	gen aux_weight = (n_treat_s/n_ec_s) if ever_faster1 == 0 & aux_touse == 1 & sample == 1
	replace aux_weight = 1 if ever_faster1 == 1 & aux_touse == 1 & sample == 1

	sum ever_faster1 if ever_faster1 == 1 & touse == 1 & sample == 1
	local n_treat = `r(N)'

	sum ever_faster1 if ever_faster1 == 0 & touse == 1 & sample == 1
	local n_ec = `r(N)' 

	gen weight = 1 if ever_faster1 == 1 & touse == 1 & sample == 1
	replace weight = `n_ec'/`n_treat'*aux_weight if ever_faster1 == 0 & touse == 1 & sample == 1

	tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

	qui reghdfe ln_empl_bet faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(robust)
	tabstat weight if e(sample), c(s) s(N sum)

	bys group_strata: egen num = total(touse * sample)
	egen flg_strata = tag(group_strata)
	tabstat num if flg_strata == 1, c(s) s(N mean sd min p5 p10 q p90 p95 max)

	drop aux_weight 
	drop n_treat_s n_ec_s
	drop tmp*
	cap drop num flg_strata

	// Run regression on bootstrapped sample

	foreach var of varlist `var1' {

		qui reghdfe `var' faster1 if touse == 1 [aweight=weight], absorb(betnr county_year) vce(cluster ao_kreis)

		// Save results in matrix; saving coefficient, standard error, p-value and number of observations
		matrix m_`var' = (m_`var' \ _b[faster1], _se[faster1], ///
								2*ttail(e(df_r), abs(_b[faster1]/_se[faster1])), e(N))
	

	}
	}
	restore

}


foreach var of varlist `var1' {

	matrix list m_`var'
	svmat double m_`var', name(`var')

	drop `var'3 `var'4
	rename `var'1 `var'_beta
	rename `var'2 `var'_se
	}

local allcorrs `c1' `c2' `c3' `c4'
local allserrs `s1' `s2' `s3' `s4'

***	Table D.12, top right, P-values Romano-Wolf
rwolf ln_prdt_bet_beta avg_lnw_bot_bet_beta count_mgmt_bet_beta shr_bloss_w_bet_beta, ///
			nobootstraps stdests(ln_prdt_bet_se avg_lnw_bot_bet_se count_mgmt_bet_se shr_bloss_w_bet_se) pointestimates(`allcorrs') stderrs(`allserrs')

log close
